package com.pyue.eam.regi.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pyue.eam.admin.api.entity.regi.CqTaskDayEntity;
import com.pyue.eam.admin.api.entity.regi.CqTaskLibEntity;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.admin.utils.EamDateUtil;
import com.pyue.eam.admin.utils.HolidayUtil;
import com.pyue.eam.regi.mapper.CqTaskDayMapper;
import com.pyue.eam.regi.mapper.CqTaskLibMapper;
import com.pyue.eam.regi.service.CqTaskDayService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 任务日报
 *
 * @date 2023-12-23 08:19:25
 */
@Service
@AllArgsConstructor
public class CqTaskDayServiceImpl extends ServiceImpl<CqTaskDayMapper, CqTaskDayEntity> implements CqTaskDayService {
    private final HolidayUtil holidayUtil;
    private final CqTaskLibMapper cqTaskLibMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveData(CqTaskDayEntity cqTaskDay) {
        CqTaskLibEntity cqTaskLibEntity = cqTaskLibMapper.selectById(cqTaskDay.getTaskId());
        ApiAsserts.isTrue(cqTaskLibEntity != null, "任务不存在");
        // 任务日报的费用计算
        assert cqTaskLibEntity != null;
        wrapTaskDayPrice(cqTaskDay, cqTaskLibEntity.getWorkHour());

        save(cqTaskDay);
        return cqTaskDay.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDayReportById(CqTaskDayEntity cqTaskDay) {
        CqTaskLibEntity cqTaskLibEntity = cqTaskLibMapper.selectById(cqTaskDay.getTaskId());
        ApiAsserts.isTrue(cqTaskLibEntity != null, "任务不存在");
        // 任务日报的费用计算
        assert cqTaskLibEntity != null;
        wrapTaskDayPrice(cqTaskDay, cqTaskLibEntity.getWorkHour());

        // 更新任务费用
        return updateById(cqTaskDay);
    }

    /**
     * 任务日报的费用计算
     *
     * @param cqTaskDay 日报内容
     * @param workHour  小时工费
     */
    private void wrapTaskDayPrice(CqTaskDayEntity cqTaskDay, Double workHour) {
        // 基础参数
        LocalDate workDay = EamDateUtil.parseStrToLocalDate(cqTaskDay.getDayTime());
        ApiAsserts.isTrue(workDay != null, "工作日期不能为空");

        LocalTime start = LocalTime.parse(cqTaskDay.getStartTime());
        LocalTime end = LocalTime.parse(cqTaskDay.getEndTime());
        Double workHourPrice = Convert.toDouble(workHour, 0D);
        ApiAsserts.isTrue(workHourPrice > 0D, "工时费不能为空");
        // 每工时费
        cqTaskDay.setWorkHour(workHour);

        // 总工时
        Double countHour = HolidayUtil.workHourCount(LocalDateTime.of(workDay, start), LocalDateTime.of(workDay, end));
        cqTaskDay.setCountHour(countHour);

        // 加班工时
        Double overworkHour = holidayUtil.overTimeHour(start, end);
        cqTaskDay.setOverworkHour(overworkHour);

        // 正常工时
        Double workRangePrice = countHour - overworkHour;
        cqTaskDay.setWorkRangeHour(workRangePrice);

        // 是否假期
        if (holidayUtil.isPlusPriceDay(workDay)) {
            // 节假日备注
            cqTaskDay.setHolidayRemark(holidayUtil.holidayNameByDate(workDay));
            // 正常工时费
            cqTaskDay.setWorkRangePrice(workRangePrice * workHourPrice * 1.5);
        } else {
            // 正常工时费
            cqTaskDay.setWorkRangePrice(workRangePrice * workHourPrice);
        }
        // 加班工时费
        cqTaskDay.setOverworkPrice(overworkHour * workHourPrice * 1.5);
        // 总工时费
        cqTaskDay.setCountWorkerPrice(cqTaskDay.getWorkRangePrice() + cqTaskDay.getOverworkPrice());
    }
}

